﻿Imports System.Runtime.InteropServices
Public Class ProgrammingEnv
    'WARNING THIS APP CONTAINS SOME ANNOYING FUCTIONALITY 
    'WHICH YOU MAY WANT TO COMMENT OUT WHILE MAKING CHANGES / TESTING

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    'Function to map drive/connect to drive
    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String) As Boolean

        Dim nr As NETRESOURCE


        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        nr.lpLocalName = DriveLetter & ":"
        strUsername = txb_Domain.Text & "\" & txb_User.Text '(add parameters to pass this if necessary)
        'txtDomain.Text & "\" & 
        strPassword = Txb_Pass.Text '(add parameters to pass this if necessary)



        'lblUserName.Text = strUsername

        nr.dwType = RESOURCETYPE_DISK

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        If result = 0 Then
            Return True

        Else
            Return False
        End If



    End Function
    'Kill Explorer process - second attempt

    'Function to disconnect from drive
    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer
        rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

        If rc = 0 Then
            Return True
            'messageBox.Show("The network drive could not be mapped for the following reason..dammit!")
        Else
            Return False
            'messageBox.Show("The network drive was mapped ok?")
        End If

    End Function
    Public Sub RestartExplorer()
        System.Diagnostics.Process.Start("explorer.exe")
    End Sub


    Public Sub KillExplorer()

        Dim taskKill As ProcessStartInfo = New ProcessStartInfo("taskkill", "/F /IM explorer.exe")
        taskKill.WindowStyle = ProcessWindowStyle.Hidden
        Dim Process As Process = New Process()
        Process.StartInfo = taskKill
        Process.Start()
        Process.WaitForExit()

    End Sub

    Private Sub ProgrammingEnv_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'KILL OFF WINDOWS EXPLORER WHEREVER SHE MAY BE HIDING
        KillExplorer()

        Dim strHostName As String
        strHostName = System.Net.Dns.GetHostName()
        LblCompName.Text = strHostName


        UnMapDrive("N")
        'UnMapDrive("Z")
        ' strusername = "hasla\" & txb_User.Text
        'strpassword = Txb_Pass.Text


        'Kill Explorer process - OLD CODE
        'Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Explorer.exe")
        'For Each p As Process In pProcess
        '    p.Kill()
        'Next




        'Make form full-screen
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.Location = New Point(0, 0)
        Me.Size = SystemInformation.PrimaryMonitorSize



    End Sub


    ' Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Connect drive X
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'MessageBox.Show("Attempting to map drive now....")

        Try
            'Usage: mapdrive(driveletter, uncpath, username, password). Use "Nothing" without quotes if username or password are blank
            ' MapDrive("Z", "\\8454611-FS02\StaffFolders$\" & txb_User.Text, txb_User.Text, Txb_Pass.Text)
            'MapDrive("Z", "\\8454611-FS02\StaffFolders$\" & txb_User.Text, txb_User.Text, Txb_Pass.Text)

            MapDrive("N", "\\8454612-fs01\studentsfolders$\" & txb_User.Text, txb_User.Text, Txb_Pass.Text)
        Catch ex As Exception
            MessageBox.Show("The network drive could not be mapped for the following reason: " & ex.Message)
        End Try
        Try

            Process.Start("C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\WDExpress.exe")
            'Process.Start("notepad.exe")
        Catch ex As Exception
            MessageBox.Show("Could not launch Visual Studio for the following reason: " & ex.Message)
        End Try

        'Dim ProfileDirectory As System.IO.DirectoryInfo
        If My.Computer.FileSystem.DirectoryExists("Z:\RPF") Then
            MessageBox.Show("Home Drive mapped ok, starting Visual Studio...")
            'ProfileDirectory = My.Computer.FileSystem.GetDirectoryInfo("N:\RPF")
            Me.Close()
        Else
            MessageBox.Show("Something went wrong. Did you type your username/password correctly?")
        End If
        'Me.Close()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Process.Start("shutdown", "-l")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Process.Start("shutdown", "-s")

    End Sub

    Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
        'RestartExplorer()
    End Sub

    Private Sub LblCompName_Click(sender As Object, e As EventArgs) Handles LblCompName.Click
        'RestartExplorer()

    End Sub
End Class
